package com.pig4cloud.pigx.shop.user.common.mq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pig4cloud.pigx.common.core.constant.enums.OperEnum;
import com.pig4cloud.pigx.common.core.constant.enums.PointChangeSourceEnum;
import com.pig4cloud.pigx.common.core.constant.enums.PointChangeStatusEnum;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserInfo;
import com.pig4cloud.pigx.shop.user.api.entity.ShopUserPointChangeRecord;
import com.pig4cloud.pigx.shop.user.service.ShopUserInfoService;
import com.pig4cloud.pigx.shop.user.service.ShopUserPointChangeRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
@RocketMQMessageListener(topic = "shop-user-point-change", consumerGroup = "shop-user-point-group")
public class ShopUserPointChangeListener implements RocketMQListener<String> {

	private final ShopUserPointChangeRecordService shopUserPointChangeRecordService;

	private final ShopUserInfoService shopUserInfoService;

	/**
	 * 积分变更记录
	 * @param id
	 */
	@Override
	public void onMessage(String id) {
		log.info("积分变更信息: {}", id);
		try {
			// Long id = data.getId();
			Long pointChangeId = Long.parseLong(id);
			ShopUserPointChangeRecord pointChangeRecord = shopUserPointChangeRecordService.getById(pointChangeId);
			if (pointChangeRecord != null) {
				if (!pointChangeRecord.getStatus().equals(PointChangeStatusEnum.PROCESSING.getType())) {
					log.info("积分变更记录状态不是处理中状态: {}", id);
					return;
				}
				ShopUserInfo shopUserInfo = shopUserInfoService.getOne(Wrappers.<ShopUserInfo>lambdaQuery()
						.eq(ShopUserInfo::getShopUserId, pointChangeRecord.getShopUserId()));
				if (shopUserInfo != null) {
					ShopUserInfo userInfo = new ShopUserInfo();
					userInfo.setId(shopUserInfo.getId());
					Integer pointBalance = shopUserInfo.getPointBalance();
					log.info("积分变更记录ID:{}, 用户ID:{}, 变更前积分:{}", pointChangeId, shopUserInfo.getShopUserId(),
							pointBalance);
					if (pointChangeRecord.getOper().equals(OperEnum.ADD.getType())) {
						// 添加操作
						pointBalance += pointChangeRecord.getPoint();
					}
					else {
						// 减少操作
						pointBalance -= pointChangeRecord.getPoint();
					}
					userInfo.setPointBalance(pointBalance);
					shopUserInfoService.updateById(userInfo);
					// 修改积分变更记录状态为成功
					pointChangeRecord.setStatus(PointChangeStatusEnum.SUCCESS.getType());
					shopUserPointChangeRecordService.updateById(pointChangeRecord);
					log.info("积分变更记录ID:{}, 用户ID:{}, 变更后积分:{}", pointChangeId, shopUserInfo.getShopUserId(),
							pointBalance);
				}
				else {
					log.info("用户不存在:" + pointChangeId + "无法上积分");
				}
			}
			else {
				log.info("不存在当前积分变更记录:" + pointChangeId + "无法上积分");
			}

		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
